package com.kingcat.smartchair;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class SmartDatabaseHelper extends SQLiteOpenHelper {

    private static final String create_log = "create table deviceLog(id integer primary key autoincrement, udid text, page text, time integer,cat integer, event text, message, text)";
    private Context mContext;

    public SmartDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
        mContext = context;
    }

    // 创建数据库（SmartDatabaseHelper构造函数版 数据库不存在时系统自动调用）
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(create_log);
    }

    // 创建数据库（SmartDatabaseHelper构造函数版本参数与上一次不一致时 自动调用）
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    // 查询所有日志
    @SuppressLint("Range")
    public static List<ContentValues> QueryDeviceLog(Context context)
    {
        List<ContentValues> res = new ArrayList<>();
        SmartDatabaseHelper dbHelper = new SmartDatabaseHelper(context, "SmartStore.db", null, 1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor  = db.query("deviceLog", null, null, null, null, null, null);
        if(cursor.moveToFirst())
        {
            do{
                ContentValues temp = new ContentValues();
                temp.put("id",cursor.getLong(cursor.getColumnIndex("id")));
                temp.put("udid",cursor.getString(cursor.getColumnIndex("udid")));
                temp.put("page",cursor.getString(cursor.getColumnIndex("page")));
                temp.put("time",cursor.getLong(cursor.getColumnIndex("time")));
                temp.put("cat",cursor.getLong(cursor.getColumnIndex("cat")));
                temp.put("event",cursor.getString(cursor.getColumnIndex("event")));
                temp.put("message",cursor.getString(cursor.getColumnIndex("message")));
                res.add(temp);
            }while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        dbHelper.close();

        return res;
    }

    // 插入一条日志
    public static void AddDeviceLog(Context context, ContentValues model)
    {
        SmartDatabaseHelper dbHelper = new SmartDatabaseHelper(context, "SmartStore.db", null, 1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.insert("deviceLog", null, model);

        db.close();
        dbHelper.close();
    }

    // 删除一条日志
    public static void DelDeviceLog(Context context, String id)
    {
        SmartDatabaseHelper dbHelper = new SmartDatabaseHelper(context, "SmartStore.db", null, 1);
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        db.delete("deviceLog", "id = ?", new String[]{id});

        db.close();
        dbHelper.close();
    }
}
